package com.chs.easychartwidget.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

@SuppressWarnings("ALL")
public class DateUtil {
    private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    public static final SimpleDateFormat datetimeFormatNotDay = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

    /**
     * 判断原日期是否在目标日期之后
     *
     * @param src 源日期
     * @param dst 目的日期
     * @return 返回值
     */
    public static boolean isAfter(Date src, Date dst) {
        return src.after(dst);
    }

    /**
     * 将字符串日期时间转换成java.util.Date类型
     * <p>
     * 日期时间格式yyyy-MM-dd HH:mm:ss
     *
     * @param datetime 字符串时间
     * @return 返回Date
     */
    public static Date parseDatetime(String datetime) throws ParseException {
        return datetimeFormat.parse(datetime);
    }

    /**
     * 格式化日期时间
     * <p>
     * 日期时间格式yyyy-MM-dd HH:mm:ss
     *
     * @return 返回时间格式
     */
    public static String formatDatetime(Date date) {
        return datetimeFormat.format(date);
    }

    /**
     * 获得当前日期时间
     * <p>
     * 日期时间格式yyyy-MM-dd HH:mm:ss
     *
     * @return 返回时间格式
     */
    public static String currentDatetime() {
        return datetimeFormat.format(now());
    }

    /**
     * 格式化日期时间
     *
     * @param date    时间
     * @param pattern 格式化模式，详见{@link SimpleDateFormat}构造器
     *                <code>SimpleDateFormat(String pattern)</code>
     * @return 时间
     */
    public static String formatDatetime(Date date, String pattern) {
        SimpleDateFormat customFormat = (SimpleDateFormat) datetimeFormat.clone();
        customFormat.applyPattern(pattern);
        return customFormat.format(date);
    }

    /**
     * 获得当前时间的<code>java.util.Date</code>对象
     *
     * @return 当前时间
     */
    public static Date now() {
        return new Date();
    }

    private static Calendar calendar() {
        Calendar cal = GregorianCalendar.getInstance(Locale.CHINESE);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        return cal;
    }

    /**
     * 获得当前Chinese月份
     *
     * @return 当前月份
     */
    public static int month() {
        return calendar().get(Calendar.MONTH) + 1;
    }

    /**
     * 获得当前Chinese年份
     *
     * @return 年份
     */
    public static int year() {
        return calendar().get(Calendar.YEAR);
    }


    /**
     * 将Date转为String，格式为yyyy-MM-dd
     * 2016-11-11
     */
    public static String textForDateAll(Date date) {
        return (new SimpleDateFormat("yyyy-MM-dd   HH:mm:ss", Locale.getDefault())).format(date);
    }

    /**
     * 2016.06.29
     */
    public static String textForNow3() {
        return textForNow("MM-dd HH:mm:ss");
    }

    /**
     * 用规定的patter来格式化时间
     */
    public static String textForOther(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
        return sdf.format(date);
    }

    /**
     * 默认模版显示日期
     *
     * @param pattern 正则
     * @return 日期
     */
    private static String textForNow(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
        return sdf.format(Calendar.getInstance().getTime());
    }

    /**
     * 根据规则显示message界面要显示的时间逻辑：1与当前时间进行比较，如果是今天显示几小时前/几分钟前，如果不是显示几天前
     *
     * @param date date类型时间
     * @return 字符串时间
     */
    public static String parseMessageDate(Date date) {
        return getTimeRange(date);
    }

    /**
     * 设置每个阶段时间
     */
    private static final int seconds_of_1minute = 60;
    private static final int seconds_of_30minutes = 30 * 60;
    private static final int seconds_of_1hour = 60 * 60;
    private static final int seconds_of_1day = 24 * 60 * 60;
    private static final int seconds_of_15days = seconds_of_1day * 15;
    private static final int seconds_of_30days = seconds_of_1day * 30;
    private static final int seconds_of_6months = seconds_of_30days * 6;
    private static final int seconds_of_1year = seconds_of_30days * 12;

    /**
     * 格式化时间
     *
     * @return 字符串时间
     */
    private static String getTimeRange(Date startTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        //获取当前时间
        Date curDate = new Date(System.currentTimeMillis());
        String dataStrNew = sdf.format(curDate);
        try {
            //将时间转化成Date
            curDate = sdf.parse(dataStrNew);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //除以1000是为了转换成秒
        long between = (curDate.getTime() - startTime.getTime()) / 1000;
        int elapsedTime = (int) (between);
        if (elapsedTime < seconds_of_1minute) {
            return "刚刚";
        }
        if (elapsedTime < seconds_of_30minutes) {
            return elapsedTime / seconds_of_1minute + "分钟前";
        }
        if (elapsedTime < seconds_of_1hour) {
            return "半小时前";
        }
        if (elapsedTime < seconds_of_1day) {
            return elapsedTime / seconds_of_1hour + "小时前";
        }
        if (elapsedTime < seconds_of_15days) {
            return elapsedTime / seconds_of_1day + "天前";
        }
        if (elapsedTime < seconds_of_30days) {
            return "半个月前";
        }
        if (elapsedTime < seconds_of_6months) {
            return elapsedTime / seconds_of_30days + "月前";
        }
        if (elapsedTime < seconds_of_1year) {
            return "半年前";
        }
        if (elapsedTime >= seconds_of_1year) {
            return elapsedTime / seconds_of_1year + "年前";
        }
        return "";
    }

    /**
     * 判断两个时间的时间差
     *
     * @return 返回值以天小时分方式
     */
    public static String getTimeRange(String startTime, String endTime) {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault());
        try {
            long from = simpleFormat.parse(startTime).getTime();
            long to = simpleFormat.parse(endTime).getTime();
            long diff = to - from;//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            if (days == 0 && hours == 0) {
                return minutes + "分";
            } else if (days == 0) {
                return hours + "小时" + minutes + "分";
            }
            return days + "天" + hours + "小时" + minutes + "分";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将服务器返回的英文时间(带时区的)格式化
     *
     * @param time 字符串时间
     * @return 格式化后的时间
     */
    public static String getTimeDateAll(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
        try {
            Date date = simpleDateFormat.parse(time);
            return DateUtil.textForDateAll(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return time;
        }
    }
}
